package com.lepu.flutterdemo.boost

import android.content.Intent
import android.os.Bundle
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import com.idlefish.flutterboost.FlutterBoost
import com.idlefish.flutterboost.FlutterBoostRouteOptions
import com.idlefish.flutterboost.containers.FlutterBoostActivity
import com.lepu.flutterdemo.R
import io.flutter.embedding.android.FlutterActivityLaunchConfigs
import kotlinx.android.synthetic.main.activity_boost_test.*

/**
 * @author yangqing
 * @time 2021/10/18 15:37
 * @describe
 */
class BoostTestActivity:AppCompatActivity() {
    var hashMap = HashMap<String, Any>()
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_boost_test)
        btnJumpPageOne.setOnClickListener {
            hashMap.put("name", "杨庆")
            hashMap.put("age", "19")
            val options = FlutterBoostRouteOptions.Builder()
                .opaque(true)
                .pageName("page_one")
                .arguments(hashMap)
                .build()
            FlutterBoost.instance().open(options)
        }

        btnJumpPageTwo.setOnClickListener {
            hashMap.put("name", "杨庆")
            hashMap.put("age", "19")
            var intent =
                FlutterBoostActivity.CachedEngineIntentBuilder(FlutterBoostActivity::class.java)
                    .backgroundMode(FlutterActivityLaunchConfigs.BackgroundMode.transparent)
                    .destroyEngineWithActivity(false)
                    .url("page_two")
                    .urlParams(hashMap)
                    .build(FlutterBoost.instance().currentActivity())
            startActivityForResult(intent, 10001)
        }
        btnJumpTestEntry.setOnClickListener {
            val options = FlutterBoostRouteOptions.Builder()
                .opaque(true)
                .pageName("test_entry")
                .arguments(hashMap)
                .build()
            FlutterBoost.instance().open(options)
        }
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        var reslut = data?.getSerializableExtra(com.idlefish.flutterboost.containers.FlutterActivityLaunchConfigs.ACTIVITY_RESULT_KEY)//注意导包 ACTIVITY_RESULT_KEY
        Toast.makeText(this, "onActivityResult:$reslut", Toast.LENGTH_SHORT).show()
    }
}